<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /*
    只能装 truckSize 个箱子到卡车上，根据贪心的思路，
    只需要每次都拿当前剩下的箱子里单元数量最大
    的箱子即可。对 boxTypes 按照 maxBox 
    进行逆序排序，然后从左至右填充 ruckSize 即可。
    */
    var maximumUnits = function (boxTypes, truckSize) {
      boxTypes.sort((a, b) => b[1] - a[1]);
      let res = 0;
      for (const [box, maxBox] of boxTypes) {
        if (box < truckSize) {
          res += box * maxBox;
          truckSize -= box;
        } else {
          res += truckSize * maxBox;
          break;
        }
      }
      return res;
    };
    console.log(maximumUnits(boxTypes = [[5, 10], [2, 5], [4, 7], [3, 9]], truckSize = 10))
  </script>
</body>

</html>